<script setup>
import { ref } from 'vue'
import {ElMessage} from 'element-plus'
import '@vueup/vue-quill/dist/vue-quill.snow.css'
import {
  artPublishService,
} from '@/api/article'

// 控制抽屉显示隐藏
const visibleDrawer = ref(false)

// 默认数据
const defaultForm = {
  pre_id: '', // 处方号
  doc_name:'', //医生名
  med_name: '', // 药名
  number: '',//数量
  data:'',//处方时间
  state: ''// 状态
}

// 准备数据
const formModel = ref({ ...defaultForm })

// 图片上传相关逻辑
const imgUrl = ref('')

// 提交
const emit = defineEmits(['success'])
const onPublish = async (state) => {
  // 将成功或失败状态，存入 formModel
  formModel.value.state = state

  // 注意：当前接口，需要的是 formData 对象
  // 将普通对象 => 转换成 => formData对象
  const fd = new FormData()
  for (let key in formModel.value) {
    fd.append(key, formModel.value[key])
  }
    // 添加操作
    await artPublishService(fd)
    ElMessage.success('添加成功')
    visibleDrawer.value = false
    // 通知到父组件，添加成功了
    emit('success', 'add')
}

// 组件对外暴露一个方法 open，基于open传来的参数，区分添加还是编辑
// open({})  => 表单无需渲染，说明是添加
// open({ id, ..., ... })  => 表单需要渲染，说明是编辑
// open调用后，可以打开抽屉
const editorRef = ref()
const open = async () => {
  visibleDrawer.value = true // 显示抽屉

    formModel.value = { ...defaultForm } // 基于默认的数据，重置form数据
    // 这里重置了表单的数据，但是图片上传img地址，富文本编辑器内容 => 需要手动重置
    imgUrl.value = ''
    editorRef.value.setHTML('')
}

defineExpose({
  open
})
</script>

<template>
  <el-drawer
    v-model="visibleDrawer"
    :title="'添加处方'"
    direction="rtl"
    size="50%"
  >
    <!-- 发表文章表单 -->
    <el-form :model="formModel" ref="formRef" label-width="100px">
      <el-form-item label="处方号" prop="pre_id"
                    :rules="[{ required: true, message: '请输入处方号', trigger: 'blur' }]">
        <el-input v-model="formModel.pre_id" placeholder="请输入处方号"></el-input>
      </el-form-item>
      <el-form-item label="医生名" prop="doc_name"
                    :rules="[{ required: true, message: '请输入医生名', trigger: 'blur' }]">
        <el-input v-model="formModel.doc_name" placeholder="请输入医生名"></el-input>
      </el-form-item>
      <el-form-item label="药品名称" prop="med_name"
                    :rules="[{ required: true, message: '请输入药品名称', trigger: 'blur' }]">
        <el-input v-model="formModel.med_name" placeholder="请输入药品名称"></el-input>
      </el-form-item>
      <el-form-item label="数量" prop="number"
                    :rules="[{ required: true, message: '请输入药品数量', trigger: 'blur' }]">
        <el-input v-model="formModel.number" placeholder="请输入药品数量"></el-input>
      </el-form-item>
      <el-form-item label="处方时间" prop="data"
                    :rules="[{ required: true, message: '请输入处方时间', trigger: 'blur' }]">
        <el-input v-model="formModel.data" placeholder="请输入处方时间"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="onPublish('成功')" type="primary">添加</el-button>
      </el-form-item>
    </el-form>
  </el-drawer>
</template>

<style lang="scss" scoped>
.avatar-uploader {
  :deep(1) {
    .avatar {
      width: 178px;
      height: 178px;
      display: block;
    }

    .el-upload {
      border: 1px dashed var(--el-border-color);
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
      transition: var(--el-transition-duration-fast);
    }

    .el-upload:hover {
      border-color: var(--el-color-primary);
    }

    .el-icon.avatar-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 178px;
      height: 178px;
      text-align: center;
    }
  }
}

.editor {
  width: 100%;

  :deep(.ql-editor) {
    min-height: 200px;
  }
}
</style>
